package edu.notebook.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.notebook.dao.NoteDao;
import edu.notebook.domain.Note;
import edu.notebook.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class NoteServiceImpl  extends ServiceImpl<NoteDao, Note> implements NoteService {
    public NoteServiceImpl() {
        super();
    }
    @Autowired
    private NoteDao noteDao;

    @Override
    public Boolean saveBook(Note note) {
        return noteDao.insert(note) > 0;
    }

    @Override
    public Boolean update(Note note) {
        return noteDao.updateById(note) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return noteDao.deleteById(id) > 0;
    }

    @Override
    public Note getById(Integer id) {
        return noteDao.selectById(id);
    }

    /**
     * 主页面分页查询
     * @param currentPage
     * @param pageSize
     * @param note
     * @return IPage<Note>
     */
    @Override
    public IPage<Note> getPage(int currentPage, int pageSize, Note note) {
         LambdaQueryWrapper<Note> lqw = new LambdaQueryWrapper<>();
        lqw.like(note.getTitle()!=null,Note::getTitle,note.getTitle());
        lqw.like(note.getNewsMsg()!=null,Note::getNewsMsg,note.getNewsMsg());
        lqw.eq(note.getNewsState()!=null,Note::getNewsState,note.getNewsState());
        lqw.eq(note.getConfidentLevel()==null,Note::getConfidentLevel,1);
        IPage page = new Page(currentPage,pageSize);
        System.out.println("lqw:"+lqw);
        noteDao.selectPage(page,lqw);
        return page;
    }

    @Override
    public List<Note> selectAllByConfidentLevel(int confidentLevel) {
        return noteDao.selectAllByConfidentLevel();
    }

    @Override
    public List<Note> getAll() {
        return noteDao.selectList(null);
    }
}
